/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.system.application.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Named;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.system.application.DatabaseInfoApplication;
import com.foreveross.bsl.system.application.vo.DatabaseInfoVO;
import com.foreveross.bsl.system.model.domain.DatabaseInfo;

/**
 * @description
 *
 * @author rening
 * @version v1.0
 *
 * @2013-6-7
 *
 */
@Named("databaseInfoApplication")
public class DatabaseInfoApplicationImpl implements DatabaseInfoApplication {

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#saveEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public DatabaseInfoVO saveEntity(DatabaseInfoVO entity) {
		DatabaseInfo databaseInfo = new DatabaseInfo();
		BeanHelper.copyProperties(databaseInfo, entity);
		databaseInfo.save();
		entity.setId(databaseInfo.getId());
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#updateEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public DatabaseInfoVO updateEntity(DatabaseInfoVO entity) {
		DatabaseInfo databaseInfo = new DatabaseInfo();
		BeanHelper.copyProperties(databaseInfo, entity);
		databaseInfo.save();
		BeanHelper.copyProperties(entity, databaseInfo);
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#deleteEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void deleteEntity(DatabaseInfoVO entity) {
		DatabaseInfo databaseInfo = new DatabaseInfo();
		BeanHelper.copyProperties(databaseInfo, entity);
		databaseInfo.delete();
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity(java.lang.String)
	 */
	@Override
	public DatabaseInfoVO getEntity(String id) {
		DatabaseInfo databaseInfo = new DatabaseInfo();
		databaseInfo = (DatabaseInfo)databaseInfo.findOne(id);
		DatabaseInfoVO vo = new DatabaseInfoVO();
		BeanHelper.copyProperties(vo, databaseInfo);
		return vo;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findAll()
	 */
	@Override
	public List<DatabaseInfoVO> findAll() {
		DatabaseInfo databaseInfo = new DatabaseInfo();
		Iterable iterable = databaseInfo.findAll();
		if(iterable != null){
			List<DatabaseInfoVO> result = new ArrayList<DatabaseInfoVO>();
			DatabaseInfoVO vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				DatabaseInfo obj = (DatabaseInfo) iterator.next();

				vo = new DatabaseInfoVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findByPageable(org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<DatabaseInfoVO> findByPageable(Pageable pageable) {
		DatabaseInfo databaseInfo = new DatabaseInfo();
		Page page = databaseInfo.findAll(pageable);
		if(page != null && page.getContent() != null){
			List<DatabaseInfoVO> result = new ArrayList<DatabaseInfoVO>();
			DatabaseInfoVO vo;
			for (Iterator iterator = page.getContent().iterator(); iterator.hasNext();) {
				DatabaseInfo obj = (DatabaseInfo) iterator.next();

				vo = new DatabaseInfoVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<DatabaseInfoVO> pageVO = new PageImpl<DatabaseInfoVO>(result, pageable, page.getTotalElements());
			return pageVO;
		}
		return null;
	}

}
